<template>
	<view class="content">
		
		<u--form
			class="item-text-font"
			labelPosition="left"
			label-width="110"
			:model="form"
			ref="checkInForm"
			:rules="rules"
			labelAlign="center"
		>
			<u-form-item >
				<u-search placeholder="商品名或编号查询" v-model="searchGoodeCode" :show-action="false" @search="searchGood"></u-search>
			</u-form-item>
			<u-form-item label="商品名" prop="goodName">
				<u-input :disabled="true" v-model="form.goodName"></u-input>
			</u-form-item>
			<u-form-item label="代码">
				<u-input :disabled="true" v-model="form.goodNo"></u-input>
			</u-form-item>
			
			<u-form-item label="库存">
				<u-input :disabled="true" v-model="form.inventory"></u-input>
			</u-form-item>
			<u-form-item label="货架">
				<u-input :disabled="true" v-model="form.goodShelve"></u-input>
			</u-form-item>
			
			<u-form-item label="供应商">
				<u-search :show-action="false" placeholder="点击选择" v-model="selectProviderName" :shape="square" :disabled="true" @click="showProviderPicker"></u-search>
			</u-form-item>
			<u-form-item label="入库">
				<u-number-box v-model="form.amount" :min="1" :max="99999"  :input-width="300" :input-height="60"></u-number-box>
			</u-form-item>
			<u-button @click="submitCheckIn" type="primary">提交</u-button>
		</u--form>
		
	
		<u-picker :show="isShowProviderPicker" :columns="providerList" ref="picker" title="选择供应商" keyName="providerName"
			@cancel="isShowProviderPicker = false" @confirm="confirmProvider" ></u-picker>
			
		<u-picker :show="isShowGoodPicker" :columns="searchGoodlist" keyName="nameAndNo" @confirm="confirmGood"  @cancel="isShowGoodPicker = false"  ref="goodPicker" title="请选商品" ></u-picker>	
	</view>
</template>

<script>
	import { searchGoodByNo,loadProvider ,saveGoodCheckIn} from '@/api/good.js'
	export default{
		
		data(){
			
			return {
				
				isShowProviderPicker:false,
				searchGoodeCode:"",
				selectProviderName:undefined,
				isShowGoodPicker:false,
				form:{
					goodId:undefined,
					goodName:undefined,
					goodNo:undefined,
					inventory:undefined,
					goodShelve:undefined,
					providerId:undefined,
					amount:1
				},
				providerList: [],
				searchGoodlist:[],
				rules: {
					goodName:[
						{required:true,message:"请选择商品",trigger:'blur'}
					]
				}
			}
		},
		onReady() {
			this.loadProvider();
			this.$refs.checkInForm.setRules(this.rules);
		},
		methods:{
			loadProvider(){
				let that=this;
				loadProvider().then(response=>{
					let proList=response.data;
					that.providerList=[];
					that.providerList.push(proList);
				});
			},
			searchGood(){
				
				
				let that=this;
				let sourceGoodNo=this.searchGoodeCode;
				sourceGoodNo=uni.$u.trim(sourceGoodNo)
				if(!sourceGoodNo ||sourceGoodNo===''){
					uni.showToast({
						title:'请输入商品编号进行查询',
						icon:'none'
					});
					return false;
				}
				that.searchGoodlist=[];
				searchGoodByNo({nameNoKeyWord:sourceGoodNo,goodType:'SOURCE',page:1,pageSize:20}).then(response=>{
					let searchDataList=response.data;
					if(!searchDataList || searchDataList.length===0){
						uni.showToast({
							title:'未找到任何商品',
							icon:'none'
						});
						return false;
					}
					if(searchDataList.length===1){
						that.setGoodDataToForm(searchDataList[0]);
					}else{
						that.searchGoodlist.push(response.data);
						that.isShowGoodPicker=true;
					}
					
				});
				
				
				/* let that=this;
				let sourceGoodNo=this.searchGoodeCode;
				sourceGoodNo=uni.$u.trim(sourceGoodNo)
				
				if(!sourceGoodNo ||sourceGoodNo===''){
					uni.showToast({
						title:'请输入商品编号进行查询',
						icon:'none'
					});
					return false;
				}
				 
				searchGoodByNo({goodNo:this.searchGoodeCode,goodType:'SOURCE'}).then(response=>{
					
					
					let goodList=response.data;
					if(!goodList || goodList.length==0){
						that.$u.toast("未找到相关商品");
						that.form.goodId=undefined;
						that.form.goodName=undefined;
						that.form.goodNo=undefined;
						that.form.inventory=undefined;
						that.form.goodShelve=undefined;
						return false;
					}
					let good=goodList[0];
					that.form.goodId=good.id;
					that.form.goodName=good.goodName;
					that.form.goodNo=good.goodNo;
					that.form.inventory=good.inventory;
					that.form.goodShelve=good.goodShelve;
					
				}); */
			},
			showProviderPicker(){
				
				this.isShowProviderPicker=true;
	
			},
			
			confirmProvider(e){
				this.isShowProviderPicker=false;
				//console.log("e.indexs",e.indexs,"value",e.value,"values",e.values);
				let selected=e.indexs[0];
				let provider=this.providerList[0][selected];
				this.selectProviderName=provider.providerName;
				this.form.providerId=provider.id;
			},
			confirmGood(e){
				this.isShowGoodPicker=false;
				let selected=e.indexs[0];
				let good=this.searchGoodlist[0][selected];
				this.setGoodDataToForm(good);
			},
			setGoodDataToForm(good){
				this.form.goodId=good.id;
				this.form.goodName=good.goodName;
				this.form.goodNo=good.goodNo;
				this.form.inventory=good.inventory;
				this.form.goodShelve=good.goodShelve;
			},
			submitCheckIn(){
				let that=this;
				let valid=this.validateForm();
				if(!valid){
					return false;
				}
				
				uni.showLoading({
					title:"请稍等",
					icon:"none"
				})
				saveGoodCheckIn(this.form).then(res=>{
					uni.hideLoading();
					that.$u.toast("上报成功");
					uni.navigateBack();
				}).catch(res=>{
					
				})
			},
			
			validateForm(){
				let goodId=this.form.goodId;
				
				if(!goodId || goodId==null){
					uni.showToast({
						title:'请选择商品',
						icon:'none',
						duration:2000
					})
					return false;
				}
				let providerId=this.form.providerId;
				if(!providerId || providerId==null){
					uni.showToast({
						title:'请选择供应商',
						icon:'none',
						duration:2000
					})
					return false;
				}
				let amount=this.form.amount;
				if(!amount){
					uni.showToast({
						title:'请填写入库数量',
						icon:'none',
						duration:2000
					})
					return false;
				}
				return true;
			}
			
			
		}
	}
</script>

<style scoped>
	.item-text-font{
		font-size: 12px;
	}
</style>